package tgc.rj.zz.custom;

import java.util.HashMap;
import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import tgc.rj.zz.entity.Bj;
import tgc.rj.zz.entity.Log;
import tgc.rj.zz.entity.Record;
import tgc.rj.zz.entity.Student;
import tgc.rj.zz.entity.Teacher;

public class DatagridFormUtils {
	public static HashMap<String, Object> getPageResult(Page<?> page) {
		HashMap<String, Object> result = new HashMap<>();
		result.put("total", page.getTotalElements());   //数据库中数据的总页面数
		result.put("rows", page.getContent()); //所有的数据
		result.put("page", page.getNumber()+1);   //当前页面数量
		result.put("sort", page.getSort()); //分类
		return result;
	}

	/*
	 * 学生类form和查询
	 */
	private Integer id;
	private Integer page;//第几页
	private Integer rows;//一页显示几条记录
	private String sort;//排序字段
	private String order;//排序的方式，降序or升序
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	private String name;
	private String username;
	private String sex;
	private Integer age;
	private String phone;
	private String ranmek;
	private Bj bj;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getRanmek() {
		return ranmek;
	}
	public void setRanmek(String ranmek) {
		this.ranmek = ranmek;
	}
	public Bj getBj() {
		return bj;
	}
	public void setBj(Bj bj) {
		this.bj = bj;
	}
	public Pageable buildPageable() {
		page--;
		return PageRequest.of(page, rows);   
	
}
	public Specification<Student> studentBuildSpes() {
		Specification<Student> spec = new Specification<Student>() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(name)) {
					rules.add(criteriaBuilder.like(root.get("name"), "%"+name+"%"));
				}else if(StringUtils.hasText(username)) {
					rules.add(criteriaBuilder.like(root.get("username"), "%"+username+"%"));
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}	
		};
		return spec;
	}
	
	public Specification<Teacher> teacherBuildSpes() {
		Specification<Teacher> spec = new Specification<Teacher>() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<Teacher> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(name)) {
					rules.add(criteriaBuilder.like(root.get("name"), "%"+name+"%"));
				}else if(StringUtils.hasText(username)) {
					rules.add(criteriaBuilder.like(root.get("username"), "%"+username+"%"));
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}	
		};
		return spec;
	}
	
	private String borrowedName; //借书人
	private String contactWay; //联系方式
	private String contactPhone; //联系电话
	private String date;  //借书还书日期
	private String accredit; //等待管理员授权，已借，已还
	private Integer reader; //读者id
	private String book;//图书名称
	public String getBorrowedName() {
		return borrowedName;
	}
	public void setBorrowedName(String borrowedName) {
		this.borrowedName = borrowedName;
	}
	public String getContactWay() {
		return contactWay;
	}
	public void setContactWay(String contactWay) {
		this.contactWay = contactWay;
	}
	public String getContactPhone() {
		return contactPhone;
	}
	public void setContactPhone(String contactPhone) {
		this.contactPhone = contactPhone;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getAccredit() {
		return accredit;
	}
	public void setAccredit(String accredit) {
		this.accredit = accredit;
	}
	public Integer getReader() {
		return reader;
	}
	public void setReader(Integer reader) {
		this.reader = reader;
	}
	public String getBook() {
		return book;
	}
	public void setBook(String book) {
		this.book = book;
	}
	public Specification<Record> recordBuildSpec() {
		Specification<Record> spec = new Specification<Record>() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Record> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(accredit)) {
					rules.add(criteriaBuilder.like(root.get("accredit"), "%"+accredit+"%"));
				}else if(StringUtils.hasText(date)) {
					rules.add(criteriaBuilder.like(root.get("date"), "%"+date+"%"));
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
				
				
			}
		};
		return spec;
	}
	
	private String operation;//操作事情
	public String getOperation() {
		return operation;
	}
	public void setOperation(String operation) {
		this.operation = operation;
	}
	public Specification<Log> logBuildSpes() {
		Specification<Log> spec = new Specification<Log>(){

			@Override
			public Predicate toPredicate(Root<Log> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(username)) {
					rules.add(criteriaBuilder.like(root.get("username"), "%"+username+"%"));
				}else if(StringUtils.hasText(name)){
					rules.add(criteriaBuilder.like(root.get("name"), "%"+name+"%"));
				}else if(StringUtils.hasText(operation)){
					rules.add(criteriaBuilder.like(root.get("operation"), "%"+operation+"%"));
					
				}
					return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
				}
			};
		return spec;
	}
	
	
	private String byid;  //编号
	private String filename; //图片名称
	private String uuid; //uuid
	private String title;//书名
	private String suthor; //作者
	private Integer number;//数量
	public String getByid() {
		return byid;
	}
	public void setByid(String byid) {
		this.byid = byid;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public String getUuid() {
		return uuid;
	}
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getSuthor() {
		return suthor;
	}
	public void setSuthor(String suthor) {
		this.suthor = suthor;
	}
	public Integer getNumber() {
		return number;
	}
	public void setNumber(Integer number) {
		this.number = number;
	}

	
	
	
}
